Refactored version which works nicely
Added touchscreen support