various bug-fixes, refactored trimming