Updated code

Made bug fixes and refractored code