Complete code overhaul, attempt to sort major bugs