more refactoring and cleanup, preparing for better filtering