first commit. The code is quite messy now but at least get it working.