fixed some bugs and improved readability