Working basic code (will need polishing later)