large refactoring - added functions like handleX