Cleaned up code, mostly working