removed while loops, improved error handling and node allocation