Updated to use nanosleep() instead of usleep() which requires a _BSD_SOURCE macro