removed duplicate code and merged the actions into single function