Refactored code. Should be enough for now that more productive progress can be made.