added more logging and fixed int overflow.