Fixing issues #5 and #6. Adding refined connection-time detection to main function