code refactored - work in progress