The CodeBook is one of the popular real-time background models for moving object detection in a video. However, for some of the complex scenes, it does not achieve satisfactory results due to the lack of an automatic parameters estimation mechanism. In this paper, we present an improved CodeBook model, which is robust in sudden illumination changes and quasi-periodic motions. The major contributions of the paper are a robust statistical parameter estimation method, a controlled adaptation procedure, a simple, but effective technique to suppress shadows and a novel block based approach to utilize the local spatial information. The proposed model was tested on numerous complex scenes and results shows a significant performance improvement over standard model.