code improved for performance