Fixed name of table
Added commands to save data
Split algorithm into two functions