removed code duplication and fixed meaning of length var
added support for center alignment + test