Added synchronization with NTP and re-organized code