Finished major refactor. Also fixed crash.