Program runs but does not yield correct results