fixed several issues with the code