Some refactorings, added type signatures.