Modifications (fixed bugs) and added some code for debugging