Bug fixes & modularize code