Cleaning up code and fixing Database