Added subprogram, cleaned up variables