minor logic changes and bug fix for page refresh