Initial refactor after getting things to work