cleaning up code, handle cases better