Modified old code for better reliability.