Refactor: More input validation, more tests, removed defunct code