cleaned up the code, there are still a few bugs