modified for better performance