general refactors; modularize more bits