Cleaning up code and fixing id problem