Various refactors and compiler warning fixes