Cleaned up most of code, still in a working state