Cleaned up code a bit, fixed minor issues