Added locking of lastlog files. Fixed leaks of filedescriptors.