refactor and finish multi-file code
fixes for various bugs for multi file code