Optimize some code so it is not on two location