more refactoring
needs testing on Ubuntu