Some refactoring - needs testing
Some refactoring - needs testing