modularized and cleaned code