reader: refactor code and cleanup and move around code for mudularity