Code refractoring and clean up
Code refractoring and minor bug fixes