Patched up refactor to a working state