Complete code overhaul for backwards compatibility, cleans up properly