Did some refactoring - seems to work