scattered the code in different files
and some minor structure enhancements