added support for multiple concurrent connections, fixed median function