fixed entire function to display proper result