Cleaned up a bit, fixed some bugs.