lots of refactoring. tested for ubuntu atm