Cleaned up code a little, still ugly