bugs corrected. Improved the implementation and added few comments