Added logic, now working mostly, still needs error handling