refactored for more flexibility and better design - it works :)