edited for better code practice and readability