working to requirement... but needs refactoring