does the job, needs refactoring...