Updated script for new formulae. Various bug fixes