Changed the code to minimize side effects