Completely refactor and fix