Cleaned up a bit and fixed some bugs