Added documentation and proper thread joining