Error fix + refactor/cleanup