First version. It needs to be a little refactored