Minor bugs fixed
Code refactoring