fixed bugs and improved code