modularizing code, added portno argument to cache
Nick: hopefully I've fixed the merge conflicts...