Code cleanup with a lot of core changes