Refactored some functions and further work on real-time stuff.