removed repeated code and gave functions better names