Initial commit, needs testing and refactoring